package com.tongqu.controller;

import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.REST;
import com.flickr4java.flickr.photos.Photo;
import com.flickr4java.flickr.photos.PhotoList;
import com.flickr4java.flickr.photos.PhotosInterface;
import com.flickr4java.flickr.photos.SearchParameters;
import com.tongqu.annotation.RequestMapping;
import com.tongqu.po.Request;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;


import static io.netty.buffer.Unpooled.copiedBuffer;

@Controller
public class DataController {
    @Autowired
    private RedisTemplate redisTemplate;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    final static String click ="click";
    final static String visit = "visit";
    @RequestMapping(path = "/click")
    public void click(ChannelHandlerContext channelHandlerContext, Request request) throws FlickrException {
        String sourceAppId = (String) request.getParmeter().get("sourceAppId");
        String targetAppId = (String) request.getParmeter().get("targetAppId");
        String key = click+"_"+sourceAppId+"_"+targetAppId;
        FullHttpResponse resp ;
        if(sourceAppId==null || targetAppId==null){
            resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    copiedBuffer("参数找不到", CharsetUtil.UTF_8));
            resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
            channelHandlerContext.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
        }else{

           saveIntoRedis(key);
            logger.info(sourceAppId+":"+targetAppId);
            resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    copiedBuffer("success", CharsetUtil.UTF_8));
            resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
            channelHandlerContext.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
        }

    }

    @RequestMapping(path = "/visit")
    public void visit(ChannelHandlerContext channelHandlerContext, Request request) throws FlickrException {
        String sourceAppId = (String) request.getParmeter().get("sourceAppId");
        String targetAppId = (String) request.getParmeter().get("targetAppId");
        String key = visit+"_"+sourceAppId+"_"+targetAppId;
        FullHttpResponse resp ;
        if(sourceAppId==null || targetAppId==null){
            resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    copiedBuffer("参数找不到", CharsetUtil.UTF_8));
            resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
            channelHandlerContext.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
        }else{
            logger.info(sourceAppId+":"+targetAppId);
            saveIntoRedis(key);
            resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    copiedBuffer("success", CharsetUtil.UTF_8));
            resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
            channelHandlerContext.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
        }


    }

    private void saveIntoRedis(String key) {
        if(redisTemplate.hasKey(key)){
            Long l = Long.parseLong((String) redisTemplate.opsForValue().get(key));
            l++;
            redisTemplate.opsForValue().set(key,l+"");
        }else{
            redisTemplate.opsForValue().set(key,1L+"");
        }
    }

}
